import rest from '@/utils/rest'
import useRouteUtil from '@/hooks/useRouteUtil'
import { gp } from '@gp'
type Options = {
  res: string
  id: string
  autoLoad: {
    type: boolean
    default: true
  }
}

export default function (options: Options) {
  const res = options.res
  const autoLoad = options.autoLoad == undefined ? true : options.autoLoad

  const { goBack } = useRouteUtil()
  const data = reactive({
    loading: false,
    row: {},
    title: {},
    listUrl: '',
  })

  onMounted(async () => {
    if (autoLoad) {
      const id = route.query.id
      if (id == undefined) {
        gp.$baseMessage('页面参数id为空', 'error', 'vab-hey-message-error')
        return false
      }
      await loadDetail(id)
    }
  })

  const route = useRoute()
  const loadDetail = async (id: any) => {
    data.loading = true
    const { data: row } = await rest.get(res, id)
    data.row = row
    data.loading = false
  }

  return {
    data,
    loadDetail,
    goBack,
  }
}
